package csu.rjxyg.service.impl;

import java.util.List;

import csu.rjxyg.action.constant.InfoConstant;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.dao.BaseDaoAdapter;
import csu.rjxyg.factory.core.BeanFactory;
import csu.rjxyg.factory.core.DaoFactory;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseInfoService;
import csu.rjxyg.util.LogUtil;
import csu.rjxyg.util.SortUtil;

public class InfoService implements BaseInfoService {
	private BeanFactory factory;
	private LogUtil log;
	private SortUtil sort;
	
	
	public InfoService(){
		factory = BeanFactory.getBeanFactory();
		log = UtilFactory.getLogUtil(getClass());
		sort = UtilFactory.getSortUtil();
	}
	
	
	@Override
	public BaseList getAllInfoById(List list, int status) {
		// TODO Auto-generated method stub
		BaseList baseList = (BaseList) factory.getBean(BaseBean.BEAN_LIST);
		try{
			BaseDaoAdapter infoDAO = DaoFactory.getDaoByColName(InfoConstant.COLLECTION_NAME);
			for(int i = 0;i < list.size();i++){
				BaseObject object = infoDAO.getBeanInfoById((String) list.get(i));
				baseList.add(object);
			}
			
			//如果是收件箱请求则按信息状态排序
			if( status == 1 )
				baseList = sort.sort(baseList, InfoConstant.INFO_STATUS, 1);
		}catch(Exception e){
			log.info("调用infoDAO的getBeanInfoById方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		
		return baseList;
	}

	
	@Override
	public boolean insertInfo(BaseObject infoBean){
		try{
			BaseDaoAdapter infoDAO = DaoFactory.getDaoByColName(InfoConstant.COLLECTION_NAME);
			return infoDAO.insertBean(infoBean);
			
		}catch(Exception e){
			log.info("调用infoDAO的insertBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}
	
	@Override
	public boolean changeInfoStatus(String infoId, String status){
		try{
			BaseDaoAdapter infoDAO = DaoFactory.getDaoByColName(InfoConstant.COLLECTION_NAME);
			BaseObject object = infoDAO.getBeanInfoById(infoId);
			object.set(InfoConstant.INFO_STATUS, status);
			return infoDAO.updateBean(infoId, object);
			
		}catch(Exception e){
			log.info("调用infoDAO的updateBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}
	
	@Override
	public boolean changeInfoStatus(String[] infoId, String status){
		try{
			BaseDaoAdapter infoDAO = DaoFactory.getDaoByColName(InfoConstant.COLLECTION_NAME);
			for( int i = 0; i < infoId.length;i++ ){
				BaseObject object = infoDAO.getBeanInfoById(infoId[i]);
				object.set(InfoConstant.INFO_STATUS, status);
				infoDAO.updateBean(infoId[i], object);
			}			
			return true;
			
		}catch(Exception e){
			log.info("调用infoDAO的updateBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}


	@Override
	public boolean deleteInfoInUserById(String infoId) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter infoDAO = DaoFactory.getDaoByColName(InfoConstant.COLLECTION_NAME);	
			return infoDAO.deleteBean(infoId);
		}catch(Exception e){
			log.info("调用userDAO的deleteBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}


	@Override
	public boolean deleteInfoInUserById(String[] infoId) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter infoDAO = DaoFactory.getDaoByColName(InfoConstant.COLLECTION_NAME);	
			return infoDAO.deleteBean(infoId);
		}catch(Exception e){
			log.info("调用userDAO的deleteBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}


	
	
}
